package org.shilong.UI;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.shilong.MContext.WebRequest.WebRequestContext;
import org.shilong.Model.FriendModel;
import org.shilong.Utility.constants.APIConstants;
import org.shilong.Utility.constants.WeiboConstants;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;

import com.weibo.net.AccessToken;
import com.weibo.net.Oauth2AccessTokenHeader;
import com.weibo.net.Utility;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboException;
import com.weibo.net.WeiboParameters;

public class WeiboTest extends Activity {

	private String uidStr;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.tttttt);

		handler.post(new MyJob());
	}

	private Handler handler = new Handler();

	private class MyJob implements Runnable {

		@Override
		public void run(){
				// TODO Auto-generated method stub

				Weibo mWeibo = Weibo.getInstance();

				mWeibo.setupConsumerConfig(WeiboConstants.APIKEY,
						WeiboConstants.APISECRET);

				mWeibo.setRedirectUrl(WeiboConstants.CALLBACK_URL);

				AccessToken token = new AccessToken(
						"2.00DCkNxCGv3e5E94ed9a159eOBWTAD",
						WeiboConstants.APISECRET);
				token.setExpiresIn("75315");

				mWeibo.setAccessToken(token);

				WeiboParameters params =  new WeiboParameters();

				params.add( "source", WeiboConstants.APIKEY );

				Utility.setAuthorization(new Oauth2AccessTokenHeader());  //	加上之后就不报出现过期的等异常了

				//获取互粉好友
				try {
					String friendsRlt = Weibo.getInstance().request(WeiboTest.this, APIConstants.GET_FRIEND,
							params, Utility.HTTPMETHOD_GET,
							Weibo.getInstance().getAccessToken());
					
					List<FriendModel> friendList = new ArrayList<FriendModel>();
					friendList = getIdsList( friendsRlt );
					
					String rlt = getJsonRlt2( friendList );
					
					String add_rtl = WebRequestContext.createInstance( WebRequestContext.TYPE_ADD_WEIBO_FRIEND ).getWebRequestRlt( "44",rlt );
					
				} catch (WeiboException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				
				
				
				
			
				
			}
		} 

	private List<FriendModel> getIdsList( String rlt ){

		List<FriendModel> friList = new ArrayList<FriendModel>();
		
		try {
			JSONObject obj = new JSONObject(rlt);
			
			String IDARR = obj.getString("ids");
			

			IDARR  = IDARR.replace("[", "");
			IDARR  = IDARR.replace("]", "");

			String[] arr = IDARR.split(",");

			for (String string : arr) {

				Weibo mWeibo = Weibo.getInstance();

				WeiboParameters params = new WeiboParameters();
				params.add( "uid", string );

				Utility.setAuthorization( new  Oauth2AccessTokenHeader() );
				
				String re = mWeibo.request( WeiboTest.this , "https://api.weibo.com/2/users/show.json", params, Utility.HTTPMETHOD_GET, mWeibo.getAccessToken());

				JSONObject oo = new JSONObject( re );

				FriendModel model = new FriendModel();

				model.setWeibo_id( oo.getString("id"));
				model.setScreen_name( oo.getString("screen_name"));
				model.setProfile_image_url( oo.getString("profile_image_url"));
				model.setLocation( oo.getString(  "location"  ) );
				model.setDescription( oo.getString( "description" ));
				
				friList.add(model);
			}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return friList;


}

/**
 * 新的
 * @param list
 * @return
 * @throws JSONException 
 */
private String getJsonRlt2( List<FriendModel> list ) throws JSONException{
	
	JSONObject obj = new JSONObject();
	
	JSONArray arr = new JSONArray();
	
	Iterator<FriendModel> it = list.iterator();
	
	while (it.hasNext()) {
		FriendModel friendModel = (FriendModel) it.next();
		
		JSONObject oo = new JSONObject();
		
		
		
		oo.put("desc", friendModel.getDescription());
		oo.put("location", friendModel.getLocation());
		oo.put("image_url", friendModel.getProfile_image_url() + ".jpg" );
		oo.put("screen_name", friendModel.getScreen_name());
		oo.put("weibo_id", friendModel.getWeibo_id());
		
		arr.put( oo );
		
	}
	
	
	obj.put("rlt", arr);
	
	System.out.println( "rlt2=====>" + obj.toString());
	
	return obj.toString();
}
	


}
